package csc.fresher.view;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.commons.EncryptMD5;
import csc.fresher.entity.SystemUser;
import csc.fresher.entity.SystemUserHasRole;
import csc.fresher.service.SystemUserHasRoleService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("request")
public class SystemUserManageBean implements Serializable{
	private static final long serialVersionUID = 1L;

	@Autowired
	private SystemUserService systemUserService;
	@Autowired
	private SystemUserHasRoleService systemUserHRService;
	@Autowired
	private SystemLoginManageBean systemLoginManageBean;
	
	//public SystemUserManageBean(){}
	private final String email_pattern = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$";
	private final String phone_pattern = "^\\s*\\d+\\s*$";
	private final String idCardNumber_pattern = "^\\s*\\d+\\s*$";
	
	private String userName;
	private String password;
	private String idCardNumber;
	private String firstName;
	private String lastName;
	private String midName;
	private String address;
	private String address2;
	private String phone;
	private String phone2;
	private String email;
	private String email2;
	

	
	private boolean userAdmin;
	private boolean userSupport;
	private boolean accountAdmin;
	private boolean accountSupport;
	private boolean reportSupport;

	
	private static Pattern pattern;
	private static Matcher matcher;
	private static String emailStatus;
	private static String phoneStatus;
	private static String emailStatus2;
	private static String phoneStatus2;
	private static String idCardNumberStatus;
	
//	public void test(){
//		System.out.println("aggaga");
//	}
	
	
	public String submit() {
		SystemUser systemUser = new SystemUser();
		SystemUserHasRole systemUserHR = new SystemUserHasRole();
		
		
		systemUser.setUserName(userName);
		systemUser.setPassWord(EncryptMD5.md5(password));
		systemUser.setIdCardNumber(idCardNumber);
		systemUser.setFirstName(firstName);
		systemUser.setMidName(midName);
		systemUser.setLastName(lastName);
		systemUser.setAddress(address);
		systemUser.setAddress2(address2);
		systemUser.setPhoneNumber(phone);
		systemUser.setPhoneNumber2(phone2);
		systemUser.setEmail(email);
		systemUser.setEmail2(email2);
		
		systemUser.setSystemUserCreateID(systemLoginManageBean.getSystemUser().getSystemUserID());
		systemUser.setSystemUserActiveID(systemLoginManageBean.getSystemUser().getSystemUserID());
		systemUser.setCreatedDate(new Timestamp(System.currentTimeMillis()));
		systemUser.setLastUpdate(new Timestamp(System.currentTimeMillis()));
		long insertResult = systemUserService.createSystemUser(systemUser);
		
		List<Long> listRole = getAllRole();
		for(int i= 0; i < listRole.size(); i ++){
			systemUserHR.setSystemUserID(insertResult);
			systemUserHR.setRoleID(listRole.get(i));
			systemUserHRService.insertUserHasRole(systemUserHR);
		}
		if(insertResult == 0) {
			return "CreateUser";
		} else {
			System.out.println("insert done");
			
			return "CreateUser";
		}
	}
	public  List<Long> getAllRole()
	{
		List<Long> listRole = new ArrayList<Long>();
		if(userAdmin == true)
			listRole.add(1L);
		if(userSupport == true)
			listRole.add(2L);
		if(accountAdmin == true)
			listRole.add(3L);
		if(accountSupport == true)
			listRole.add(4L);
		if(reportSupport == true)
			listRole.add(5L);
		return listRole;
	}
	
	
	public String cancel(){
		return "CreateUser";
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getIdCardNumber() {
		return idCardNumber;
	}
	public void setIdCardNumber(String idCardNumber) {
		this.idCardNumber = idCardNumber;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getMidName() {
		return midName;
	}
	public void setMidName(String midName) {
		this.midName = midName;
	}

	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getAddress2() {
		return address2;
	}
	public void setAddress2(String address2) {
		this.address2 = address2;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getPhone2() {
		return phone2;
	}
	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getEmail2() {
		return email2;
	}
	public void setEmail2(String email2) {
		this.email2 = email2;
	}
	
	public String editProfile(){
		return ("AdminModify?faces-redirect=true");
	}
	
	
	
	public boolean isUserAdmin() {
		return userAdmin;
	}
	public void setUserAdmin(boolean userAdmin) {
		this.userAdmin = userAdmin;
	}
	public boolean isUserSupport() {
		return userSupport;
	}
	public void setUserSupport(boolean userSupport) {
		this.userSupport = userSupport;
	}
	public boolean isAccountAdmin() {
		return accountAdmin;
	}
	public void setAccountAdmin(boolean accountAdmin) {
		this.accountAdmin = accountAdmin;
	}
	public boolean isAccountSupport() {
		return accountSupport;
	}
	public void setAccountSupport(boolean accountSupport) {
		this.accountSupport = accountSupport;
	}
	public boolean isReportSupport() {
		return reportSupport;
	}
	public void setReportSupport(boolean reportSupport) {
		this.reportSupport = reportSupport;
	}
	
}
